/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entitydao;

import database.Database;
import entity.Autor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lattis
 */
public class AutorDAO extends TableDAO {

    private static final String FIND_BY_ID = "SELECT * FROM LIB_AUTOR WHERE ID_AUTOR = ?";
    private static final String TABLE_NAME = "LIB_AUTOR";
    private static final String FIND_ALL = "SELECT * FROM LIB_AUTOR";
    private static final String FIND_BY_SURNAME = "SELECT * FROM LIB_AUTOR WHERE PRIJMENI LIKE ? ";
    private static final String SELECY_SURNAME = "SELECT PRIJMENI FROM LIB_AUTOR WHERE ROWNUM < 500";
    private static final String SELECT_ID = "SELECT ID_AUTOR FROM LIB_AUTOR";
    private static final String DELETE_AUTOR = "DELETE FROM LIB_AUTOR WHERE ID_AUTOR = ?";
    private static final String INSERT_AUTOR = "INSERT INTO LIB_AUTOR(JMENO,PRIJMENI) VALUES(?,?)";
    private Connection mConnection;
    benchmarkapplication.StopWatch st = new benchmarkapplication.StopWatch();

    public AutorDAO(Database database) {
        super(database, TABLE_NAME);
        mConnection = database.Connection();
    }

    public boolean update(Autor autor) {
//        mConnection.setTransactionIsolation(mConnection.TRANSACTION_READ_COMMITTED);
        return false;
    }

    public boolean delete(int id_autor) {
        PreparedStatement pstm = null;
        boolean ret = false;
        try {
            pstm = mConnection.prepareStatement(DELETE_AUTOR);
            pstm.setInt(1, id_autor);
            mDatabase.BeginTransaction();
            mConnection.setTransactionIsolation(mConnection.TRANSACTION_SERIALIZABLE);
            if (!pstm.execute()) {
                ret = true;
            }
        } catch (SQLException ex) {
            mDatabase.Rollback();
            ret = false;
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
            return ret;
        }
    }

    public boolean insertAutor(Autor autor) {
        PreparedStatement pstm = null;
        boolean ret = false;
        int rownum = 0;
        try {
            pstm = mConnection.prepareStatement(INSERT_AUTOR);
            pstm.setString(1, autor.getJmeno());
            pstm.setString(2, autor.getPrijmeni());
            mDatabase.BeginTransaction();
            mConnection.setTransactionIsolation(mConnection.TRANSACTION_READ_COMMITTED);
            rownum = pstm.executeUpdate();
            if (rownum != 0) {
                ret = false;
            }
        } catch (SQLException e) {
            mDatabase.Rollback();
//            System.out.println("Rollback");
            ret = false;
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
            return ret;
        }
    }

    public List<Autor> findBySurname(String SURNAME) {
        List<Autor> listAutor = null;

        PreparedStatement pstm = null;
        ResultSet rs = null;

        try {
            pstm = mConnection.prepareStatement(FIND_BY_SURNAME);
            pstm.setString(1, "%" + SURNAME + "%");
            rs = pstm.executeQuery();
            listAutor = read(rs);
        } catch (SQLException e) {
            listAutor = null;
        } finally {
            close(rs, pstm);
            return listAutor;
        }
    }

    public List<Autor> findById(int ID) {
        List<Autor> listAutor = null;

        PreparedStatement pstm = null;
        ResultSet rs = null;

        try {
            pstm = mConnection.prepareStatement(FIND_BY_ID);
            pstm.setInt(1, ID);
            rs = pstm.executeQuery();
            listAutor = read(rs);
        } catch (SQLException e) {
            listAutor = null;
        } finally {
            close(rs, pstm);
            return listAutor;
        }
    }

    public List<Autor> findAll() {
        List<Autor> listAutor = new ArrayList<Autor>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            pstm = mConnection.prepareStatement(FIND_ALL);
            rs = pstm.executeQuery();
//            st.start();
            while (rs.next()) {
                Autor autor = new Autor(rs.getInt(1), rs.getString(2), rs.getString(3));
                listAutor.add(autor);
            }
//            st.stop();
//            System.err.println(st.getElapsedTimeSecs());
        } catch (SQLException ex) {
            listAutor = null;
        } finally {
            close(rs, pstm);
            return listAutor;
        }
    }

    private List<Autor> read(ResultSet rs) throws SQLException {
        List<Autor> listAutor = new ArrayList<Autor>();
        while (rs.next()) {
            Autor autor = new Autor(rs.getInt(1), rs.getString(2), rs.getString(3));
            listAutor.add(autor);
        }
        return listAutor;
    }

    private void close(ResultSet rs, PreparedStatement pstm) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstm != null) {
                pstm.close();
            }
        } catch (Exception e) {
            System.err.println("Closing Error");
        }
    }

    public List<String> getDictSurname() {
        List<String> listSurname = new ArrayList<String>();

        PreparedStatement pstm = null;
        ResultSet rs = null;

        try {
            pstm = mConnection.prepareStatement(SELECY_SURNAME);
            rs = pstm.executeQuery();

            while (rs.next()) {
                listSurname.add(rs.getString("prijmeni"));
            }
        } catch (SQLException ex) {
            listSurname = null;
        } finally {
            close(rs, pstm);
            return listSurname;
        }
    }

    public List<Integer> getDictId() {
        List<Integer> listId = new ArrayList<Integer>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            pstm = mConnection.prepareStatement(SELECT_ID);
            rs = pstm.executeQuery();
            while (rs.next()) {
                listId.add(rs.getInt("id_autor"));
            }
        } catch (SQLException e) {
            listId = null;
        } finally {
            close(rs, pstm);
            return listId;
        }
    }
}
